﻿using System;
using System.Data;
using System.Windows.Forms;
using OrionApplication.OrionLib;
using OrionApplication.OrionLib.Dao;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionForm
{
    public partial class OrionTableForm : OrionBaseDoubleForm
    {
        public String IdentA = "";

        public OrionTableForm() 
            : base()
        {
            dao = new DaoOrionTable();
            InitializeComponent();
        }

        public OrionTableForm(String TitreP)
            : base(TitreP)
        {
            dao = new DaoOrionTable();
            InitializeComponent();
        }

        public override void RempliKDgv()
        {
            DataSet ds = new DataSet();
            if (this.IdentA == string.Empty)
                ds = dao.SelectAllDSView();
            else
                ds = ((DaoOrionTable)dao).SelectByIdentDS(this.IdentA);

            kDgv.DataSource = ds.Tables[0];
            kDgv.Columns["OrionId"].Visible = false;
        }

        private void RempliForm(OrionTable ot)
        {
            kCbIdent.Text = ot.Ident;
            kTbText1.Text = ot.Text1;
            kTbText2.Text = ot.Text2;
            kTbText3.Text = ot.Text3;

            kNudDouble1.Value = Convert.ToDecimal(ot.Double1);
            kNudDouble2.Value = Convert.ToDecimal(ot.Double2);
            kNudDouble3.Value = Convert.ToDecimal(ot.Double3);

            if (ot.Date1 == null)
                kDtDate1.Checked = false;
            else
            {
                kDtDate1.Checked = true;
                kDtDate1.Value = ot.Date1.Value;
            }
            if (ot.Date2 == null)
                kDtDate2.Checked = false;
            else
            {
                kDtDate2.Checked = true;
                kDtDate2.Value = ot.Date2.Value;
            }
            if (ot.Date3 == null)
                kDtDate3.Checked = false;
            else
            {
                kDtDate3.Checked = true;
                kDtDate3.Value = ot.Date3.Value;
            }
        }

        private OrionTable RempliObject()
        {
            OrionTable retour = new OrionTable();
            retour.Ident = kCbIdent.Text;
            retour.Text1 = kTbText1.Text;
            retour.Text2 = kTbText2.Text;
            retour.Text3 = kTbText3.Text;

            retour.Double1 = Convert.ToDouble(kNudDouble1.Value);
            retour.Double2 = Convert.ToDouble(kNudDouble2.Value);
            retour.Double3 = Convert.ToDouble(kNudDouble3.Value);

            if (kDtDate1.Checked)
                retour.Date1 = kDtDate1.Value;
            else
                retour.Date1 = null;
            if (kDtDate2.Checked)
                retour.Date2 = kDtDate1.Value;
            else
                retour.Date2 = null;
            if (kDtDate3.Checked)
                retour.Date3 = kDtDate1.Value;
            else
                retour.Date3 = null;

            return retour;
        }

        private void RempliCbIdent()
        {
            kCbIdent.DataSource = ((DaoOrionTable)dao).SelectDistinctIdent();
        }

        private void SauvegardeForm()
        {
            OrionTable ot = this.RempliObject();
            if (kLblMode.Text == OrionConfBase.OrionModeInsert)
                ot = ((DaoOrionTable)dao).Insert(ot);
            else
            {
                if (kDgv.SelectedRows.Count > 0)
                {

                    ot.OrionId = Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value);
                    ot = ((DaoOrionTable)dao).Update(ot);

                }
                else
                    OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
            }
            this.RempliKDgv();
            this.FocusSur(ot.OrionId);
        }

        private void DupliqueEnregistrement()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                if (OrionOutils.ChoixOuiNon(OrionConfBase.ConfirmDuplique) == DialogResult.Yes)
                {
                    OrionTable ot = ((DaoOrionTable)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                    ot = ((DaoOrionTable)dao).Insert(ot);
                    this.RempliKDgv();
                    this.FocusSur(ot.OrionId);
                }
            }
            else
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
        }

        public void RowEnter()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                OrionTable om = ((DaoOrionTable)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                this.RempliForm(om);
            }
        }

        private void btnSpecActuIdent_Click(object sender, EventArgs e)
        {
            RempliCbIdent();
        }
    }
}
